package com.ucode.cgms.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ucode.cgms.mapper.CgmsGiftRewardOrderMapper;
import com.ucode.cgms.service.CgmsGiftRewardOrderService;
import com.ucode.model.cgms.CgmsGiftRewardOrder;
import com.ucode.model.cgms.enums.CgmsPayStateEnum;
import com.ucode.model.cgms.enums.CgmsSysResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.tool.util.IdentifyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class CgmsGiftRewardOrderServiceImpl implements CgmsGiftRewardOrderService{

    @Resource
    private CgmsGiftRewardOrderMapper cgmsGiftRewardOrderMapper;

    @Override
    public CgmsGiftRewardOrderMapper initMapper() {
        return cgmsGiftRewardOrderMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(CgmsGiftRewardOrder cgmsCompanionOrder) {
        if(ObjUtil.isNull(cgmsCompanionOrder.getId())){
            cgmsCompanionOrder.setId(IdentifyUtils.getDefaultSnowflakeId());
        }
        int i = cgmsGiftRewardOrderMapper.insert(cgmsCompanionOrder);
        return i > 0 ? cgmsCompanionOrder.getId() : null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySuccess(Long orderId, Long payOrderId, String payType, LocalDateTime payTime, Integer payAmount) {
        CgmsGiftRewardOrder giftRewardOrder = this.validate(orderId);

        //判断订单支付状态
        if(!ObjUtil.equals(giftRewardOrder.getPayState(), CgmsPayStateEnum.AWAIT.getState())){
            throw new UcodeServiceException(CgmsSysResultCodeEnum.REWARD_ORDER_STATUS_IS_NOT_WAITING);
        }
        //判断金额
        if(NumberUtil.mul(giftRewardOrder.getAmount(),100).intValue() != payAmount.intValue()){
            throw new UcodeServiceException(CgmsSysResultCodeEnum.REWARD_ORDER_AMOUNT_NOT_EQUALS);
        }
        //直接找陪玩点单，直接进入待确认状态；否则进入接单中状态
        Integer payState = CgmsPayStateEnum.SUCCESS.getState();
        cgmsGiftRewardOrderMapper.paySuccess(orderId, payState,payOrderId,  payType, payTime);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancel(Long id) {
        this.validate(id);
        int i = cgmsGiftRewardOrderMapper.cancel(id);
        if(i == 0)
            throw new UcodeServiceException(CgmsSysResultCodeEnum.REWARD_ORDER_STATUS_IS_NOT_WAITING);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelByMember(Long userId, Long id) {
        this.validate(id,userId);
        int i = cgmsGiftRewardOrderMapper.cancel(id);
        if(i == 0)
            throw new UcodeServiceException(CgmsSysResultCodeEnum.REWARD_ORDER_STATUS_IS_NOT_WAITING);
    }

    @Override
    public int deleteByMember(Long userId, Long id) {
        CgmsGiftRewardOrder giftRewardOrder = this.validate(id,userId);
        //校验接单状态
        if(!ObjUtil.equals(giftRewardOrder.getPayState(), CgmsPayStateEnum.AWAIT.getState())){
            throw new UcodeServiceException(CgmsSysResultCodeEnum.REWARD_ORDER_STATUS_IS_NOT_REJECTED);
        }
        return cgmsGiftRewardOrderMapper.deleteByPrimaryKey(id);
    }

    /**
     * 获得自身的代理对象，解决 AOP 生效问题
     *
     * @return 自己
     */
    private CgmsGiftRewardOrderServiceImpl getSelf() {
        return SpringUtil.getBean(getClass());
    }

}
